package cn.edu.pet.servlet.user;

import cn.edu.pet.entity.AdoptRecord;
import cn.edu.pet.entity.Pets;
import cn.edu.pet.entity.User;
import cn.edu.pet.repo.AdoptRecordRepo;
import cn.edu.pet.repo.PetRepo;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/my/adopt")
public class MyAdoptServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        User user = (User) req.getSession().getAttribute("user");
        List<AdoptRecord> adoptRecords = AdoptRecordRepo.getInstance().selectListWithUser(user.getId());
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(adoptRecords);
        PrintWriter writer = resp.getWriter();
        writer.write(json);
    }
}
